package com.tarim.streetdance.bean;

import com.tarim.streetdance.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 *
 * @Package: com.tarim.streetdance.bean
 * @ClassName: LoginInterceptor
 * @Description:拦截器
 */
@Component
public class KeepSessionInterceptor extends HandlerInterceptorAdapter {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisUtil redisUtil;

    //标示符：表示当前用户未登录(可根据自己项目需要改为json样式)
    String ERROR = "请求数据被篡改";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String requestURI = request.getRequestURI();
        logger.info("请求uri" + requestURI);
        String token = request.getHeader("token");
        if (token == null) {
            response.setHeader("token", null);
            return true;
        }
        response.setHeader("token", token);
        try {
            Object object = redisUtil.get(token);
            if (object == null) {
                response.setHeader("token", null);
            }
        }catch (Exception ex) {
            response.setHeader("token", null);
        }
        return true;
    }
}